package com.yao.longhan.net.download;

import com.yao.longhan.BuildConfig;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by heyao on 2017/7/18.
 */

public class DownloadClient {
    private static Retrofit mRetrofit;

    private static Retrofit retrofitClient() {
        if (mRetrofit == null) {
            synchronized (DownloadClient.class) {
                if (mRetrofit == null) {
                    OkHttpClient.Builder builder = new OkHttpClient.Builder();
                    if (BuildConfig.DEBUG) {
                        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
                        builder.addInterceptor(loggingInterceptor);
                    }
                    builder.connectTimeout(30, TimeUnit.SECONDS);
                    builder.readTimeout(30, TimeUnit.SECONDS);
                    builder.writeTimeout(30, TimeUnit.SECONDS);
                    OkHttpClient okHttpClient = builder.build();

                    Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
                    retrofitBuilder.client(okHttpClient);
                    retrofitBuilder.baseUrl(BuildConfig.BASE_URL);
                    retrofitBuilder.addConverterFactory(GsonConverterFactory.create());
                    mRetrofit = retrofitBuilder.build();
                }
            }
        }
        return mRetrofit;
    }

    public static <T> T getService(Class<T> clazz) {
        if (mRetrofit == null) {
            synchronized (DownloadClient.class) {
                if (mRetrofit == null) {
                    mRetrofit = retrofitClient();
                }
            }
        }
        return mRetrofit.create(clazz);
    }
}
